package com.leetcode.questiononehundardandfifty.滑动窗口;

import java.util.HashMap;

/**
 * @author wtcode
 * @Description
 * @date 2025/3/26 - 11:20
 */
public class Answer {


    public static void main(String[] args) {
        lengthOfLongestSubstring("pwwkew");
    }

    // 长度最小的子数组
    public static int minSubArrayLen(int target, int[] nums) {
        int left = 0;
        int right = 0;
        int sum = 0;
        int ans = Integer.MAX_VALUE;
        while (right < nums.length){
            sum += nums[right];
            while (sum >= target){
                ans = Math.min(ans,right-left+1);
                sum -= nums[left];
                left++;
            }
            right++;
        }
        return  ans == Integer.MAX_VALUE ? 0 : ans;
    }

    // 无重复字符的最长子串
    public static int lengthOfLongestSubstring(String s) {
        HashMap<Character, Integer> map = new HashMap();
        int left = 0;
        int max = 0;
        for(int i = 0; i < s.length(); i++){
            if(map.containsKey(s.charAt(i))){
                left = Math.max(left,map.get(s.charAt(i)) + 1);
            }
            map.put(s.charAt(i),i);
            max = Math.max(max,i-left+1);
        }
        return max;

    }
}
